home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / vector.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  8.5 KB  |  248 lines

  1. #ifndef __VECTOR_CC
  2. #define __VECTOR_CC
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * vector.cc - Non-inline definitions for the Standard Library vector class
  8.  *
  9.  * $Id: vector.cc,v 1.3 1996/08/28 01:31:33 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58. #include <stdcomp.h>
  59.  
  60. #ifndef _RWSTD_NO_NAMESPACE
  61. namespace std {
  62. #endif
  63.  
  64. //
  65. // This requires that T have a default constructor.
  66. //
  67.  
  68. template <class T, class Allocator>
  69. void vector<T,Allocator>::resize (size_type new_size)
  70. {
  71.     T value;
  72.     if (new_size > size())
  73.         insert(end(), new_size - size(), value);
  74.     else if (new_size < size())
  75.         erase(begin() + new_size, end());
  76. }
  77.  
  78. template <class T, class Allocator>
  79. void vector<T,Allocator>::resize (size_type new_size, T value)
  80. {
  81.     if (new_size > size())
  82.         insert(end(), new_size - size(), value);
  83.     else if (new_size < size())
  84.         erase(begin() + new_size, end());
  85. }
  86.  
  87. template <class T, class Allocator>
  88. vector<T,Allocator>& vector<T,Allocator>::operator= (const vector<T,Allocator>& x)
  89. {
  90.     if (&x == this) return *this;
  91.     if (x.size() > capacity())
  92.     {
  93.         destroy(start, finish);
  94.         value_alloc_type va(the_allocator);
  95.         va.deallocate(start,end_of_storage-start);
  96.         start = va.allocate(x.end() - x.begin(),0);
  97.         end_of_storage = uninitialized_copy(x.begin(), x.end(), start);
  98.     }
  99.     else if (size() >= x.size())
  100.     {
  101.         iterator i = copy(x.begin(), x.end(), begin());
  102.         destroy(i, finish);
  103.     }
  104.     else
  105.     {
  106.         copy(x.begin(), x.begin() + size(), begin());
  107.         uninitialized_copy(x.begin() + size(), x.end(), begin() + size());
  108.     }
  109.     finish = begin() + x.size();
  110.     return *this;
  111. }
  112.  
  113. template <class T, class Allocator>
  114. void vector<T,Allocator>::insert_aux (
  115.        vector<T,Allocator>::iterator position, const T& x)
  116. {
  117.     if (finish != end_of_storage)
  118.     {
  119.         value_alloc_type(the_allocator).construct(finish, *(finish - 1));
  120.         copy_backward(position, finish - 1, finish);
  121.         *position = x;
  122.         ++finish;
  123.     }
  124.     else
  125.     {
  126.         //
  127.         // We always allocate enough space for a number of additional
  128.         // elements in the vector, unless the size of each element is
  129.         // very large.
  130.         //
  131.         value_alloc_type va(the_allocator);
  132.         size_type len = size() + buffer_size;
  133.         iterator tmp = va.allocate(len,start);
  134.         uninitialized_copy(begin(), position, tmp);
  135.         va.construct((tmp + (position - begin())), x);
  136.         uninitialized_copy(position, end(), tmp + (position - begin()) + 1); 
  137.         destroy(begin(), end());
  138.         va.deallocate(begin(),end_of_storage-begin());
  139.         end_of_storage = tmp + len;
  140.         finish = tmp + size() + 1;
  141.         start = tmp;
  142.     }
  143. }
  144.  
  145. template <class T, class Allocator>
  146. void vector<T,Allocator>::insert_aux (
  147.          vector<T,Allocator>::iterator position, size_type n, const T& x)
  148. {
  149.     if (n == 0) return;
  150.     if ((size_type)(end_of_storage - finish) >= n)
  151.     {
  152.         if ((size_type)(end() - position) > n)
  153.         {
  154.             uninitialized_copy(end() - n, end(), end());
  155.             copy_backward(position, end() - n, end());
  156. #ifdef _RWSTD_FILL_NAME_CLASH
  157.             std_fill(position, position + n, x);
  158. #else
  159.             fill(position, position + n, x);
  160. #endif
  161.         }
  162.         else
  163.         {
  164.             uninitialized_copy(position, end(), position + n);
  165. #ifdef _RWSTD_FILL_NAME_CLASH
  166.             std_fill(position, end(), x);
  167. #else
  168.             fill(position, end(), x);
  169. #endif
  170.             uninitialized_fill_n(end(), n - (end() - position), x);
  171.         }
  172.         finish += n;
  173.     }
  174.     else
  175.     {
  176.         value_alloc_type va(the_allocator);
  177.         size_type len = size() + max(size(), n);
  178.         iterator tmp = va.allocate(len,start);
  179.         uninitialized_copy(begin(), position, tmp);
  180.         uninitialized_fill_n(tmp + (position - begin()), n, x);
  181.         uninitialized_copy(position, end(), tmp + (position - begin() + n));
  182.         destroy(begin(), end());
  183.         va.deallocate(begin(),end_of_storage-begin());
  184.         end_of_storage = tmp + len;
  185.         finish = tmp + size() + n;
  186.         start = tmp;
  187.     }
  188. }
  189.  
  190. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  191. template<class T, class Allocator>
  192. template<class InputIterator>
  193. void vector<T,Allocator>::insert (
  194.                  vector<T,Allocator>::iterator position, 
  195.                  InputIterator first, InputIterator last)
  196. #else
  197. template<class T, class Allocator>
  198. void vector<T,Allocator>::insert (
  199.                         vector<T,Allocator>::iterator position, 
  200.                         const_iterator first,
  201.                         const_iterator last)
  202. #endif
  203. {
  204.     if (first == last) return;
  205.     size_type n;
  206.     __initialize(n, size_type(0));
  207.     distance(first, last, n);
  208.     if ((size_type)(end_of_storage - finish) >= n)
  209.     {
  210.         if ((size_type)(end() - position) > n)
  211.         {
  212.             uninitialized_copy(end() - n, end(), end());
  213.             copy_backward(position, end() - n, end());
  214.             copy(first, last, position);
  215.         }
  216.         else
  217.         {
  218.             uninitialized_copy(position, end(), position + n);
  219.             copy(first, first + (end() - position), position);
  220.             uninitialized_copy(first + (end() - position), last, end());
  221.         }
  222.         finish += n;
  223.     }
  224.     else
  225.     {
  226.         value_alloc_type va(the_allocator);
  227.         size_type len = size() + max(size(), n);
  228.         iterator tmp = va.allocate(len,start);
  229.         uninitialized_copy(begin(), position, tmp);
  230.         uninitialized_copy(first, last, tmp + (position - begin()));
  231.         uninitialized_copy(position, end(), tmp + (position - begin() + n));
  232.         destroy(begin(), end());
  233.         va.deallocate(begin(),end_of_storage-begin());
  234.         end_of_storage = tmp + len;
  235.         finish = tmp + size() + n;
  236.         start = tmp;
  237.     }
  238. }
  239.  
  240.  
  241.  
  242. #ifndef _RWSTD_NO_NAMESPACE
  243. }
  244. #endif
  245.  
  246. #pragma option pop
  247. #endif /* __VECTOR_CC */
  248.